<?php


namespace Frieza\Modules\Oauthadmin\Controllers;


use App\Http\Controllers\Controller;
use App\Pockets\GridDataModel as pGridDataModel;
use Doraemon\cache\User as UserCache;
use Doraemon\cache\DefaultAvatar;
use Doraemon\service\Member as sMember;
use Doraemon\service\Member\AvatarCheck;
use Doraemon\service\User;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Grid\Displayers\Actions;
use Encore\Admin\Layout\Content;
use Encore\Admin\Widgets\Tab;
use Illuminate\Http\Request;

class AvatarCheckController extends Controller
{
    public function index(Request $request, Content $content){

        $requestData = $request->all();
        //dump($requestData);
        $lists = AvatarCheck::getUserInfos($requestData);
        //dump($lists);

        $gridModel = new pGridDataModel($lists["rows"], $lists["counts"], ["perPage" => $request->input("per_page", 20)]);

        $listGrid = Admin::grid($gridModel, function(Grid $grid){
            $grid->disableCreateButton();
            $grid->disableExport();
            $grid->disableRowSelector();
            $grid->disableColumnSelector();
            $grid->expandFilter();

            $grid->filter(function ($filter){
                $filter->disableIdFilter();

                $filter->column(6, function ($filter){
                    $filter->equal("uid", "用户uid")->placeholder("请输入UID");
                });

                $filter->column(9, function ($filter){
                    $filter->between("addtime", "注册时间")->datetime();
                });

                $filter->expand();

            });

            $grid->column("avatar", "头像")->display(function(){
                $userInfo = sMember::getUserInfoByUid($this->uid);
                $avatarPicUrl = User::getUserAvatarPicUrl($this->uid, $userInfo["avatar_version"]);
                return '<img src = "' .$avatarPicUrl[0].'"width = 45px heigh = 45px/>';
            });

            $grid->column("nick", "昵称")->display(function(){
               return "<a href='/admin/user/Detail/$this->uid' target='_blank'>" . $this->nick . "</a>";
            });

            $grid->actions(function (Actions $actions){
                $actions->disableEdit();
                $actions->disableDelete();
                $actions->disableView();

                $actions->prepend('<a class="fa reload-data received-package" data-uid="'.$actions->row->uid.'" href="javascript:;" " >置为默认</a>');
            });

            Admin::script($this->Submit());

        });

        $tab = new Tab();
        $tab->add("头像审核列表", $listGrid->render());
        $content->row($tab);
        return $content->header("头像审核")->breadcrumb(["text" => "头像审核"]);
    }

    public function setDefault(Request $request){
        $requestData = $request->all();
        $uid = $requestData['uid'];

        sMember::setUserDefaultAvatar($uid);
        UserCache::deleteUserInfo($uid);
        DefaultAvatar::set($uid, 0);

        $data = [
            "code" => "1",
            "msg" => "设置成功"
        ];

        return $data;
    }

    public function Submit()
    {
        return <<<SCRIPT
           
            $('.received-package').unbind('click').click(function() {
            var uid = $(this).data('uid');
            swal({
                title: "恢复默认?",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "确认",
                showLoaderOnConfirm: true,
                cancelButtonText: "取消",
                preConfirm: function() {
                        $.post('setDefault', {'uid':uid}, function (data) {
                             $.pjax.reload('#pjax-container');
                             if(data.code == 1) {
                                swal(data.msg, '', 'success');
                             } else {
                                swal(data.msg, '', 'error');
                             }                                                                                                                                    
                        });
                }
            });
        });
SCRIPT;
    }
}